/* File: sendsound.c
 * Author: mezzo forte
 * Purpose: provides code for sending sine waves to the AIC3204
			using dma
 * Last Edit: 2/17/2010 : Added this block
 */

#include "data_types.h"
#include "register_dma.h"
#include "register_cpu.h"
#include "sendsound.h"


#define XMIT_BUFF_SIZE  120
#define AUTO_RELOAD     1
#define SINE_WAVE       2


Int16 wave[XMIT_BUFF_SIZE] = {
1151,2300,3442,4574,5694,6798,7884,8948,9988,11000,11982,12931,13845,14721,15556,16349,17097,17798,18451,19053,
19602,20098,20539,20923,21250,21519,21729,21879,21970,22000,21970,21879,21729,21519,21250,20923,20539,20098,19602,19053,
18451,17798,17097,16349,15556,14721,13845,12931,11982,11000,9988,8948,7884,6798,5694,4574,3442,2300,1151,0,
-1151,-2300,-3442,-4574,-5694,-6798,-7884,-8948,-9988,-11000,-11982,-12931,-13845,-14721,-15556,-16349,-17097,-17798,-18451,-19053,
-19602,-20098,-20539,-20923,-21250,-21519,-21729,-21879,-21970,-22000,-21970,-21879,-21729,-21519,-21250,-20923,-20539,-20098,-19602,-19053,
-18451,-17798,-17097,-16349,-15556,-14721,-13845,-12931,-11982,-11000,-9988,-8948,-7884,-6798,-5694,-4574,-3442,-2300,-1151,-0
};

Int16 PING[XMIT_BUFF_SIZE*4];
Int16 PONG[XMIT_BUFF_SIZE*4];
Uint16 PINGvals;
Uint16 PONGvals;



Uint16 DMA_Count =0, Error_Count_L =0, Error_Count_R =0;
Uint16 DMA_Done = 0;

void fillPING(Uint16 Freq)
{
	
    Uint16 sample;
    Uint16 samposc, s;

    
    samposc = ((4*48000)/Freq);
    s=0;
    for ( sample = 0 ; sample < samposc ; sample++ )
    {
		/* Write sine table samples */
		s = (XMIT_BUFF_SIZE*sample)/samposc;
        PING[sample] = (wave[s]);

     	
    }
  
	PINGvals = samposc;
	
}
void fillPONG(Uint16 Freq)
{
	
	
    Uint16 sample;
    Uint16 samposc, s;
  

    samposc = ((4*48000)/Freq);
    s=0;
    for ( sample = 0 ; sample < samposc ; sample++ )
    {
		/* Write sine table samples */
		s = (XMIT_BUFF_SIZE*sample)/samposc;
        PONG[sample] = (wave[s]);

     	
    }
  
	PONGvals = samposc;
	
}
Uint16 send_ping_ch0_i2s0()
{
	Uint16 temp; 
	Uint32 add;

    DMA0_CH0_TC_LSW = PINGvals*2;

    DMA0_CH0_TC_MSW = 0x3204;     //src incre, destination fix, sync, auto  

                
	temp = DMA0_CH10_EVENT_SRC;
    DMA0_CH10_EVENT_SRC = temp | 0x0001;   // I2S2 transmit event
	add = (Uint32)PING;        

    
    add = (add<<1) + 0x10000;		// change word address to byte address and add DARAM offset for DMA
    DMA0_CH0_SRC_LSW = (Uint16)add;
    
    DMA0_CH0_SRC_MSW = 0xFFFF & (add >> 16);
    
    DMA0_CH0_DST_LSW = 0x2808;      // is20 transmit left data register lsw
    
//  DMA starts   
    temp = DMA0_CH0_TC_MSW;
	temp |= 0x8000;
	DMA0_CH0_TC_MSW = temp;

    return SUCCESS;
}
Uint16 send_ping_ch1_i2s0()
{
	Uint16 temp;
	Uint32 add;

    DMA0_CH1_TC_LSW = PINGvals*2;


         
    DMA0_CH1_TC_MSW = 0x3204;     //src incre, destination fix, sync, auto

            
	temp = DMA1_CH10_EVENT_SRC;
    DMA0_CH10_EVENT_SRC = temp | 0x0100;   // I2S2 transmit event
    

    add = (Uint32)PING;        
 
    add = (add<<1) + 0x10000;		//// change word address to byte address and add DARAM offset for DMA
    DMA0_CH1_SRC_LSW = (Uint16)add;
    
    DMA0_CH1_SRC_MSW = 0xFFFF & (add >> 16);
    
    DMA0_CH1_DST_LSW = 0x280C;      // is20 transmit right data register lsw

// DMA starts    
    temp = DMA0_CH1_TC_MSW;
	temp |= 0x8000;
	DMA0_CH1_TC_MSW = temp;
	 return SUCCESS;
	
}
//////////////////////////////////////////////////
Uint16 send_pong_ch0_i2s0()
{
	Uint16 temp; 
	Uint32 add;

    DMA0_CH0_TC_LSW = PONGvals*2;

#if(AUTO_RELOAD ==1)        
    DMA0_CH0_TC_MSW = 0x3204;     //src incre, destination fix, sync, auto  
#else
    DMA0_CH0_TC_MSW = 0x2204;     //src incre, destination fix, sync, No auto  
#endif
                
	temp = DMA0_CH10_EVENT_SRC;
    DMA0_CH10_EVENT_SRC = temp | 0x0001;   // I2S2 transmit event
	add = (Uint32)PONG;        

    
    add = (add<<1) + 0x10000;		// change word address to byte address and add DARAM offset for DMA
    DMA0_CH0_SRC_LSW = (Uint16)add;
    
    DMA0_CH0_SRC_MSW = 0xFFFF & (add >> 16);
    
    DMA0_CH0_DST_LSW = 0x2808;      // is20 transmit left data register lsw
    
//  DMA starts   
    temp = DMA0_CH0_TC_MSW;
	temp |= 0x8000;
	DMA0_CH0_TC_MSW = temp;

    return SUCCESS;
}

///////////////////////////////////////////////
Uint16 send_pong_ch1_i2s0()
{
	Uint16 temp;
	Uint32 add;

    DMA0_CH1_TC_LSW = PONGvals*2;
//    DMA0_CH1_TC_LSW = 8;

#if(AUTO_RELOAD ==1)            
    DMA0_CH1_TC_MSW = 0x3204;     //src incre, destination fix, sync, auto
#else    
    DMA0_CH1_TC_MSW = 0x2204;     //src incre, destination fix, sync, No auto
#endif
            
	temp = DMA1_CH10_EVENT_SRC;
    DMA0_CH10_EVENT_SRC = temp | 0x0100;   // I2S2 transmit event
    

    add = (Uint32)PONG;        
 
    add = (add<<1) + 0x10000;		//// change word address to byte address and add DARAM offset for DMA
    DMA0_CH1_SRC_LSW = (Uint16)add;
    
    DMA0_CH1_SRC_MSW = 0xFFFF & (add >> 16);
    
    DMA0_CH1_DST_LSW = 0x280C;      // is20 transmit right data register lsw

// DMA starts    
    temp = DMA0_CH1_TC_MSW;
	temp |= 0x8000;
	DMA0_CH1_TC_MSW = temp;
	 return SUCCESS;
	
}

void set_dma1_ch0_stop(void)
{
	Uint16 temp;
//  DMA stop
    temp = DMA1_CH0_TC_MSW;
	temp &= 0x7FFF;
	DMA1_CH0_TC_MSW = temp;    
}

void set_dma0_ch0_stop(void)
{
	Uint16 temp;
//  DMA stop
    temp = DMA0_CH0_TC_MSW;
	temp &= 0x7FFF;
	DMA0_CH0_TC_MSW = temp;    
}


void set_dma1_ch1_stop(void)
{
	Uint16 temp;
//  DMA stop
    temp = DMA1_CH1_TC_MSW;
	temp &= 0x7FFF;
	DMA1_CH1_TC_MSW = temp;    
}

void set_dma0_ch1_stop(void)
{
	Uint16 temp;
//  DMA stop
    temp = DMA0_CH1_TC_MSW;
	temp &= 0x7FFF;
	DMA0_CH1_TC_MSW = temp;    
}



void enable_dma_int(void)
{
    DMA_MSK = 0xFFFF;     // enable all interrupts
    DMA_IFR = 0xFFFF;     // clear interrupt flags
}


